﻿@model Mzg.Web.Models.AssignModel
@{
    Layout = null;
}
<!-- （Modal） -->
<div class="modal fade" id="assignedModal" tabindex="-1" role="dialog"
     aria-labelledby="assignedModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">
                    ×
                </button>
                <h4 class="modal-title" id="assignedModalLabel">
                    <span class="glyphicon glyphicon-user"></span> @app.T["assign"]
                </h4>
            </div>
            <div class="modal-body">
                <div class="row container-fluid">
                    <form class="form-horizontal" data-jsonajax="true" data-istip="true" action="/@(app.OrganizationUniqueName)/api/data/assign" method="post">
                        <input type="hidden" name="entityid" value="@Model.EntityId" />
                        <input type="hidden" name="ownerid" value="@Model.OwnerId" />
                        @{
                            int i = 0;
                            foreach (var item in Model.ObjectId)
                            {
                                <input type="hidden" data-hiddendata="true" name="objectid[@i]" value="@item" />
                                i++;
                            }
                        }
                        <div class="form-group col-sm-12">
                            <span class="glyphicon glyphicon-warning-sign"></span>
                            <span class="text-muted">@(string.Format(app.T["security_assignrecords_tip"], Model.ObjectId.Length, Model.EntityMetaData.LocalizedName))</span>
                        </div>
                        <div class="form-group col-sm-12">
                            <label><input type="radio" name="owneridtype" value="1" checked /> @app.T["assign_tome"]</label>
                        </div>
                        <div class="form-group col-sm-12">
                            <label><input type="radio" name="owneridtype" value="2" /> @app.T["assign_toother"]</label>
                            <div class="owner_selector hide">
                                <input type="text" id="ownerid" name="ownerid_text" class="form-control input-sm lookup" />
                            </div>
                        </div>
                        <div class="form-group col-sm-12">
                            <label><input type="radio" name="owneridtype" value="3" /> @app.T["assign_toteam"]</label>
                            <div class="team_selector hide">
                                <input type="text" id="teamid" name="teamid_text" class="form-control input-sm lookup" />
                            </div>
                        </div>
                    </form>
                </div>
                <script src="/content/js/jquery.form.js?v=@app.PlatformSettings.VersionNumber"></script>
                <script>
                    // 定义AssignedModel对象并有相关属性及方法
                    // mzg
                    // 202006071002
                    var AssignedModel = {
                        dialog: $('#assignedModal'),
                        dialogid: 'assignedModal',
                        entityid: '@Model.EntityId',
                        objectid: '@Model.ObjectId',
                        ownerid: '@Model.OwnerId',
                        singlemode: true,
                        initForm: function () {
                            var $form = AssignedModel.dialog.find('form');
                            // 调用表单的统一设置并返回调用结果
                            // mzg
                            // 202006071030
                            Dlv.web.Form($form, function (response) {
                                console.log(response);
                                if (response.IsSuccess == false) {
                                    AssignedModel.resetForm();//提交失败还原按钮
                                } else {
                                    AssignedModel.dialog.modal('hide');//提交成功隐藏此对象AssignedModel
                                }
                            });
                        },
                        submit: function () {//点击提交表单后按钮禁用并提交
                            AssignedModel.disabledForm();
                            var $form = AssignedModel.dialog.find('form');
                            $form.submit();
                        },
                        dialog_return: function () {
                            AssignedModel.dialog.modal('hide');
                        },
                        disabledForm: function () {
                            AssignedModel.dialog.find('button').prop('disabled', true);
                        },
                        resetForm: function () {
                            AssignedModel.dialog.find('button').prop('disabled', false);
                        },
                        // 引入对话框选择数据后在对应的input上写入选择的数据
                        // mzg
                        // 202006081626
                        bindselected: function (result, inputid) {
                            console.log(result);
                            if (!result || result.length == 0) return;
                            AssignedModel.dialog.find('#' + inputid).val(result[0].name);
                            AssignedModel.dialog.find('input[name=ownerid]').val(result[0].id);
                            //以下内容暂时取消
                            // mzg
                            // 202006081631
                            //var target = [];
                            //$(result).each(function (i, n) {
                            //    target.push(n.id);
                            //});
                            //console.log(target);
                        }
                    };
                    $(function () {
                        AssignedModel.initForm();
                        AssignedModel.dialog.modal({
                            //backdrop: 'static'
                        });
                        AssignedModel.dialog.on('hidden.bs.modal', function () {
                            Dlv.web.CloseDialog(AssignedModel.dialogid);
                        });
                        // 在定义的dialog对象中找到单选后加上click事件，并设置了在事件发生时运行的函数
                        // mzg
                        // 202006071104
                        AssignedModel.dialog.find('input[name=owneridtype]').on('click', null, function (e) {
                            var type = $(e.target).val();
                            if (type == 1) {
                                $('.owner_selector').addClass('hide');
                                $('.team_selector').addClass('hide');
                            }
                            else if (type == 2) {
                                $('.owner_selector').removeClass('hide');
                                $('.team_selector').addClass('hide');
                            }
                            else if (type == 3) {
                                $('.owner_selector').addClass('hide');
                                $('.team_selector').removeClass('hide');
                            }
                        });

                        // 设置iD为ownerid的lookup事件即可以弹出引入对话框
                        // 设置了回调函数bindselected
                        // mzg
                        // 202006081620
                        AssignedModel.dialog.find('#ownerid').lookup({
                            dialog: function () { Dlv.web.OpenDialog('/entity/RecordsDialog?entityname=systemuser&singlemode=true&inputid=ownerid', 'AssignedModel.bindselected'); }
                            , clear: function () {
                                $('#' + inputid).val('');
                                $('#' + valueid).val('');
                                return false;
                            }
                        });
                        AssignedModel.dialog.find('#teamid').lookup({
                            dialog: function () { Dlv.web.OpenDialog('/entity/RecordsDialog?entityname=team&singlemode=true&inputid=teamid', 'AssignedModel.bindselected'); }
                            , clear: function () {
                                $('#' + inputid).val('');
                                $('#' + valueid).val('');
                                return false;
                            }
                        });
                        AssignedModel.dialog.find('form').ajaxForm(function (response) {
                            if (response.IsSuccess) {
                                Dlv.web.Alert(true, response.Content);
                                AssignedModel.dialog_return();
                                AssignedModel.resetForm();
                                Dlv.web.Event.publish('refresh');
                                location.reload(true);
                                return;
                            }
                            Dlv.web.Alert(false, response.Content);
                            AssignedModel.resetForm();
                        });
                    });
                </script>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    <span class="glyphicon glyphicon-remove"></span>
                    @app.T["dialog_close"]
                </button>
                <button type="button" class="btn btn-primary" onclick="AssignedModel.submit()">
                    <span class="glyphicon glyphicon-ok"></span>
                    @app.T["dialog_ok"]
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->